﻿using System;
using System.Collections.Generic;
using System.Text;
using SalePartial.IDAL;
using DataLayerAgent;
using System.Data.SqlClient;
using System.Data;
using SalePartial.Utility;
using SalePartial.Model;

namespace SalePartial.SQLServerDAL
{
    class Categories : ICategories
    {
        public int AddCategory(CategoriesInfo cat)
        {

            SqlConnection conPortal = new SqlConnection(GlobalSetting.ConnectionString);
            SqlCommand cmd = new SqlCommand("XunPan_WareCubeAddCategory", conPortal);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(new SqlParameter("@categoryID", cat.ID));
            cmd.Parameters.Add(new SqlParameter("@name", cat.Name));
            cmd.Parameters.Add(new SqlParameter("@isParent", SqlDbType.Bit)).Value = cat.IsParent;
            cmd.Parameters.Add(new SqlParameter("@parentID", cat.ParentID));
            cmd.Parameters.Add(new SqlParameter("@opSequence", cat.OpSequence));
            conPortal.Open();
            cmd.ExecuteNonQuery();
            int result = Convert.ToInt32(cmd.Parameters["@RETURN_VALUE"].Value);
            conPortal.Close();
            return result;

        }

        public CategoriesInfo GetCategory(Guid categoryID)
        {

            SqlConnection conPortal = new SqlConnection(GlobalSetting.ConnectionString);
            SqlCommand cmd = new SqlCommand("XunPan_WareCubeGetCategory", conPortal);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@categoryID", categoryID));
            conPortal.Open();
            SqlDataReader dataReader = cmd.ExecuteReader();
            CategoriesInfo category = default(CategoriesInfo);
            if (dataReader.Read())
            {
                category = new CategoriesInfo(dataReader);
            }
            else
            {
                category = null;
            }
            conPortal.Close();
            return category;

        }

        public int UpdataCategory(CategoriesInfo cat)
        {

            SqlConnection conPortal = new SqlConnection(GlobalSetting.ConnectionString);
            SqlCommand cmd = new SqlCommand("XunPan_WareCubeUpdateCategory", conPortal);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(new SqlParameter("@categoryID", cat.ID));
            cmd.Parameters.Add(new SqlParameter("@name", cat.Name));
            cmd.Parameters.Add(new SqlParameter("@isParent", SqlDbType.Bit)).Value = cat.IsParent;
            cmd.Parameters.Add(new SqlParameter("@parentID", cat.ParentID));
            cmd.Parameters.Add(new SqlParameter("@opSequence", cat.OpSequence));
            conPortal.Open();
            cmd.ExecuteNonQuery();
            int result = Convert.ToInt32(cmd.Parameters["@RETURN_VALUE"].Value);
            conPortal.Close();
            return result;

        }

        public int DeleteCategory(Guid categoryID)
        {

            SqlConnection conPortal = new SqlConnection(GlobalSetting.ConnectionString);
            SqlCommand cmd = new SqlCommand("XunPan_WareCubeDeleteCategory", conPortal);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@categoryID", categoryID));
            conPortal.Open();
            int result = cmd.ExecuteNonQuery();
            conPortal.Close();
            return result;

        }

        public int AddCatWac(Guid catID, Guid wacID)
        {

            SqlConnection conPortal = new SqlConnection(GlobalSetting.ConnectionString);
            SqlCommand cmd = new SqlCommand("XunPan_WareCubeAddCategoryWareCube", conPortal);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(new SqlParameter("@categoryID", catID));
            cmd.Parameters.Add(new SqlParameter("@wareCubeID", wacID));
            conPortal.Open();
            cmd.ExecuteNonQuery();
            int result = Convert.ToInt32(cmd.Parameters["@RETURN_VALUE"].Value);
            conPortal.Close();
            return result;
        }
    }
}
